package com.wutong.teacher_server.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.wutong.teacher_server.datasource.DataSourceType;
import com.wutong.teacher_server.datasource.DynamicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Create By Administrator on 2020/6/12.
 */
@Configuration
public class MyBatisPlusConfig {

    private final static Logger logger = LoggerFactory.getLogger(MyBatisPlusConfig.class);




    @Bean("wuDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.wudevicedb")
    public DataSource wuDataSource() {
        DataSource build = DataSourceBuilder.create().build();
        return build;
    }

    @Bean("eduDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.edudevicedb")
    public DataSource eduDataSource(){
        return DataSourceBuilder.create().build();
    }


    @Bean
    public DynamicDataSource dataSource(@Qualifier("wuDataSource") DataSource wuDataSource,
                                        @Qualifier("eduDataSource") DataSource eduDataSource
    ) {
        Map<Object, Object> map = new HashMap<>();
        map.put(DataSourceType.test_wu_db, wuDataSource);
        map.put(DataSourceType.edu_project_db, eduDataSource);



        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(map);
        dynamicDataSource.setDefaultTargetDataSource(eduDataSource);

        return dynamicDataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DynamicDataSource dynamicDataSource){
        return new DataSourceTransactionManager(dynamicDataSource);
    }

    // wu库事务管理器
    @Bean("wuTransaction")
    @Qualifier("wuTransaction")
    public PlatformTransactionManager wuTransactionManager(@Qualifier("wuDataSource") DataSource wuDataSource) {
        return new DataSourceTransactionManager(wuDataSource);
    }

    // edu库事务管理器
    @Bean("eduTransaction")
    @Qualifier("eduTransaction")
    public PlatformTransactionManager eduTransactionManager(@Qualifier("eduDataSource") DataSource eduDataSource) {
        return new DataSourceTransactionManager(eduDataSource);
    }


    /**
     * @description: 配置分页插件
     *
     * @author: gradual
     * @date: 2019/1/15 10:17
     * @param: []
     * @return: com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        return paginationInterceptor;
    }




}